package com.lzh.exer;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author liuzhihao
 * @create 2021-02-21 10:28
 */
public class EmployeeTest {

    //按生日排序
    @Test
    public void test2(){


        TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
            //使用泛型之前的写法
            @Override
            public int compare(Employee o1, Employee o2) {

                    MyDate b1 = o1.getBirthday();
                    MyDate b2 = o2.getBirthday();

                    //方式一：
//                    int minusYear = b1.getYear() -b2.getYear();
//                    if(minusYear != 0){
//                        return minusYear;
//                    }
//
//                    int minusMonth = b1.getMonth() - b2.getMonth();
//                    if(minusMonth != 0){
//                        return minusMonth;
//                    }
//
//                    return b1.getDay() - b2.getDay();

                    //方式二
                    return  b1.compareTo(b2);

                }
//                return 0;


            //使用泛型之前的写法：
//            @Override
//            public int compare(Object o1, Object o2) {
//                if(o1 instanceof Employee && o2 instanceof Employee){
//                    Employee e1 = (Employee) o1;
//                    Employee e2 = (Employee) o2;
//
//                    MyDate b1 = e1.getBirthday();
//                    MyDate b2 = e2.getBirthday();
//
//                    //方式一：
////                    int minusYear = b1.getYear() -b2.getYear();
////                    if(minusYear != 0){
////                        return minusYear;
////                    }
////
////                    int minusMonth = b1.getMonth() - b2.getMonth();
////                    if(minusMonth != 0){
////                        return minusMonth;
////                    }
////
////                    return b1.getDay() - b2.getDay();
//
//                    //方式二
//                    return  b1.compareTo(b2);
//
//                }
////                return 0;
//                throw new RuntimeException("传入的数据不一致！");
//            }
        });

        Employee e1 = new Employee("liuzhihao",22,new MyDate(1998,9,17));
        Employee e2 = new Employee("yinjiaqi",20,new MyDate(2000,9,17));
        Employee e3 = new Employee("zhangqin",21,new MyDate(1999,10,22));
        Employee e4 = new Employee("zhangtian",21,new MyDate(1998,11,23));
        Employee e5 = new Employee("liuxiaoqian",40,new MyDate(1998,8,17));


        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator<Employee> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());

        }
    }

    //问题一：使用自然排序
    @Test
    public void test1(){
        TreeSet<Employee> set = new TreeSet<>();

        Employee e1 = new Employee("liuzhihao",22,new MyDate(1998,9,17));
        Employee e2 = new Employee("yinjiaqi",20,new MyDate(2000,7,8));
        Employee e3 = new Employee("zhangqin",21,new MyDate(1999,10,22));
        Employee e4 = new Employee("zhangtian",21,new MyDate(1998,11,23));
        Employee e5 = new Employee("liuxiaoqian",40,new MyDate(1998,8,17));


        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        Iterator<Employee> it = set.iterator();
        while(it.hasNext()){
            Employee e = it.next();
            System.out.println(e);

        }

    }

}
